﻿using lib;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace WindowsFormsApplication1
{
    public class Client
    {
        private Thread m_workerthread;
        private List<string> gegevens = new List<string>();
        private List<String> chat;
        public SecureConnectionClient secureConnection { get; set; }
        private Form3 f3;

        public Client(Form3 f)
        {
            chat = new List<string>();
            f3 = f;
            
        }
       

        public bool connect(string ip, int port)
        {
            if (secureConnection == null)
                try
                {
                    TcpClient tcpclient = new TcpClient(ip, port);
                    secureConnection = new SecureConnectionClient(tcpclient);
                    m_workerthread = new Thread(workerThread);
                    m_workerthread.Start();
                    return true;
                }
                catch
                {
                    return false;
                }
            else return true;
        }

        public void routeToServer(String data)
        {
            try
            {
                secureConnection.SendObject(data);
            }
            catch {}
        }

        public void routeMeasurementToServer(lib.MeasuredValues measurement)
        {
            try
            {
                secureConnection.SendObject("measurement");
                secureConnection.SendObject(measurement);
            }
            catch { }
        }

        private void workerThread()             //receiving data from server
        {
            BinaryFormatter reader  = new BinaryFormatter();
            {
                string data = null;
                while((data = secureConnection.ReadObjectAsString()) != null)
                {
                    if (data.Substring(0, 5) == "chat:")
                    {
                        chat.Add("Physician"+ data.Substring(4, data.Length-4));
                    }
                    else if (data == "done")
                    {
                            System.Windows.Forms.Application.Run(new Form2(gegevens));
                            gegevens.Clear();                                           
                    }

                    else if (data.Substring(0, 12) == "BikeCommand:")
                    {
                        String[] a = data.Split(':');
                        f3.serial.WriteLine(a[1] + a[2]);
                    }

                    else
                    {
                        gegevens.Add(data);
                    }
                    Thread.Sleep(10);
                }
            }
        }

        public List<String> getChat()
        {
            return chat;
        }

        public void sendMessage(String s)
        {
            chat.Add("Client: " + s);
            routeToServer("chat:" + s);
        }


        public void closeTcp()
        {
            secureConnection.ClosesslStream();
            m_workerthread.Abort();
            System.Diagnostics.Debug.WriteLine("closed the tcpstream");
        }
    }
}
